//
//  KXAddTypeViewController.swift
//  KXBook
//
//  Created by  on 2022/9/12.
//

import UIKit

class KXAddTypeViewController: KXViewController {
    @IBOutlet var selctBtn: KXTypeSelectBtn!
    @IBOutlet var input: KXTextField!
    @IBOutlet var collectionView: UICollectionView!
    var type:KXAddType = .pay
    var model = KXTypeModel()
    private var oldTypeName = ""
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = LS("新增类别")
        self.model.isPay = self.type == .pay ? 1 : 0
        self.oldTypeName = String(format: "%@", self.model.name)
        self.input.text = LS(self.model.name)
        weak var weakself = self
        self.selctBtn.type = self.type
        self.selctBtn.block = {type in
            weakself?.type = .pay
            weakself?.model.isPay = type == .pay ? 1 : 0
            weakself?.input.text = ""
        }
        self.collectionView.register(KXTypeCell.nib(), forCellWithReuseIdentifier: KXTypeCell.ID())
    }
    
    @IBAction func actionForBtn(_ sender: UIButton) {
        if self.input.text!.count <= 0{
            return Toast.msg(LS("请输入类别名"))
        }else if self.model.type == 0{
            return Toast.msg(LS("请选择类型"))
        }
        self.model.name = self.input.text!
        let whereStr = String(format:"where %@=%@",bg_sqlKey("name"),bg_sqlValue(self.oldTypeName))
        KXTypeModel.bg_deleteAsync(nil, where:whereStr) { success in
            self.model.bg_saveOrUpdateAsync { success in
                DispatchQueue.main.async {
                    Toast.msg(LS("保存成功"))
                    self.input.text = ""
                    self.model.type = 0
                    self.collectionView.reloadData()
                    NotifyTool.post(NotifyName.updateType)
                }
            }
        }
    }
    
    lazy var array:[String]={
        var arr = [String]()
        for i in 1...13{
            arr.append("组\(i)")
        }
        return arr
    }()
}


//MARK: - UICollectionViewDataSource
extension KXAddTypeViewController : UICollectionViewDataSource
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.array.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: KXTypeCell.ID(), for: indexPath) as! KXTypeCell
        cell.img.image = UIImage(named:self.array[indexPath.row])
        cell.bgHexColor = self.model.type != 0 && self.model.type == indexPath.row + 1 ? "#E27BC1" : "#969696"
        return cell
    }
    
}
//MARK: - UICollectionViewDelegate
extension KXAddTypeViewController : UICollectionViewDelegate
{
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.model.type = Int32(indexPath.row + 1)
        collectionView.reloadData()
    }
}

//MARK: - UICollectionViewDelegateFlowLayout
extension KXAddTypeViewController : UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        return CGSize(width: 42, height: 42)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat
    {
        return 20
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat
    {
        return 20
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
    {
        return UIEdgeInsets(top:0, left: 20, bottom: 20, right: 20)
    }
}
